﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using ZhiJiao.Models.Entities;
using ZhiJiao.Models.Entities.Enums;
using ZhiJiao.Service.Abstracts;
using ZhiJiao.Service.Dto;
using ZhiJiao.Service.Services;

namespace ZHIJIAO.Controllers
{
    public class RoleController : Controller
    {

        private readonly BackJson res;
        private readonly IRoleService _roleService;
        private readonly ICurrentUser _currentUser;

        public RoleController(RoleManager<Role> roleManager,
                              BackJson backJson,
                              IRoleService roleService,
                              ICurrentUser currentUser)
        {
            _roleService = roleService;
            res = backJson;
            _currentUser = currentUser;
        }

        /// <summary>
        ///  角色管理视图
        /// </summary>
        [Authorize]
        public IActionResult Index()
        {
            ViewBag.UserType =Convert.ToInt32(_currentUser.User.Type);
            if(_currentUser.User.Type == UserType.Instruction)
            {
                ViewBag.InsId = _currentUser.User.Instruction!.Id;
            }
            else
            {
                ViewBag.InsId = 0;
            }
            return View();
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        [HttpGet("api/role/get-role-list")]
        public async Task<IActionResult> RoleList(RoleType roleType, string roleName, long? insId)
        {
            return Json(_roleService.GetRoleList(roleType, roleName, insId));
        }


        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("api/role/add-role-submit")]
        public async Task<IActionResult> AddSubmit([FromBody] AddRloeDto model)
        {
            return Json(await _roleService.AddSubmit(model));
        }

        /// <summary>
        /// 角色修改
        /// </summary>
        [HttpPost("api/role/edit-role-submit")]
        public async Task<IActionResult> EditSubmit([FromBody]AddRloeDto model)
        {
            return Json(await _roleService.SubmitEdit(model));
        }


        [HttpGet("api/role/delete-role")]
        public async Task<IActionResult> Delete(string Id)
        {
            return Json(await _roleService.Delete(Id));
        }

        /// <summary>
        /// 获取单个角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("api/role/get-role")]
        public async Task<IActionResult> GetRole(string id)
        {
            return Json(await _roleService.GetRole(id));
        }

        [HttpGet("api/role/get-module-oprions")]
        public async Task<IActionResult> GetModules(RoleType roleType)
        {
            return Json(await _roleService.GetModules(roleType));
        }

    }
}
